package com.gxuwz.com_property.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.user.dto.UserDto;
import com.gxuwz.com_property.user.pojo.User;
import com.gxuwz.com_property.user.service.UserService;
import com.gxuwz.com_property.utils.BCryptUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户相关接口", description = "提供用户相关的 Rest API")
public class UserController {
    
    /**
     * 注册service
     * */
    @Autowired
    private UserService userService;

    @Autowired
    private ResponseDto responseDto;

    @Autowired
    private BCryptUtil bCryptUtil;

    /**
     * 添加用户
     * */
    @PostMapping("/addUserInfo")
    @ApiOperation("新增用户接口")
    public ResponseDto addUserInfo(@RequestBody User user){

        /**
         * 判断存不存在一模一样的用户
         * */
        User accounUser = userService.selectUsersById(user.getAccount());
        if (accounUser!=null){
            return ResponseDto.error("该用户名已经存在");
        }

        System.out.println("addUserInfo+User:"+user);

        String encrypt = bCryptUtil.encrypt(user.getPassword());
        System.out.println("密码加密为："+encrypt);
        user.setPassword(encrypt);
        Boolean result = this.userService.addUserInfo(user);

        if (result){
            return ResponseDto.success("添加成功");
        }else {
           return ResponseDto.error("添加失败");
        }
    }
    /**
     * 修改用户
     * */
    @ApiOperation("修改用户接口")
    @PostMapping("/updateUserInfo")
    public ResponseDto updateUserInfo(@RequestBody User user){
        if(user.getPassword() != null){
            String encrypt = bCryptUtil.encrypt(user.getPassword());
            System.out.println("密码加密为："+encrypt);
            user.setPassword(encrypt);
        }


        Boolean result = this.userService.updateUserInfo(user);
        if (result){
            return ResponseDto.success("修改成功");
        }else {
            return ResponseDto.error("修改失败");
        }
    }

    /**
     * 删除用户
     * */
    @PostMapping("/delectUsersById")
    public ResponseDto delectUserById(@RequestBody String account){
        System.out.println("delectUserById+account"+account);
        Boolean result = this.userService.delectUserById(account);
        if (result){
            return ResponseDto.success("删除成功");
        }else {
            return ResponseDto.error("删除失败");
        }

    }

    /**
     * 根据Id查询用户
     * */
    @PostMapping("/sys/personal")
    public ResponseDto selectUsersById(@RequestBody UserDto userDto){
        System.out.println(userDto.getAccount());
        User user = this.userService.selectUsersById(userDto.getAccount());
        return ResponseDto.success("200",user);
    }

    /**
     * 分页查询用户信息
     * @param userDto
     * @return
     *
     * Page<User> 使用该项，则需要导入Spring Data JPA
     */
    @PostMapping("/selectUsersByPage")
    public ResponseDto selectUsersByPage(@RequestBody UserDto userDto) {

        System.out.println("pageNo:"+userDto.getPageNo());
        System.out.println("pageSize:"+ userDto.getPageSize());
        int pageNo = userDto.getPageNo();
        int pageSize = userDto.getPageSize();

        IPage<User> userIPage = userService.selectUsersByPage(new Page<User>(pageNo, pageSize), userDto);
        return ResponseDto.success("用户查询成功",userIPage);
    }
}
